package cn.zmit.kuxi.application;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Application;
import android.os.Environment;

public class MyApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		// 监听异常
		Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

	}

	private class MyUncaughtExceptionHandler implements
			UncaughtExceptionHandler {

		@Override
		public void uncaughtException(Thread thread, Throwable ex) {
			System.out.println("异常捕获...");

			// 打印日志到sd卡
			File file = new File(Environment.getExternalStorageDirectory(),
					"carch.log");
			try {
				PrintWriter err = new PrintWriter(file);
				ex.printStackTrace(err);
				err.flush();
				err.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

			// 出现异常把app干掉重启而不是卡死在界面
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}
}
